/**
 * 通话量详情页面数据获取
 * @author 陈涛
 * @date 2019-1-14 14:25
 */
import request from '@/api/CallAmountInfo/CallAmountInfo'
import * as type from '@/store/Mutation/index'
import commonUtils from '@/assets/js/commonUtil.js'
const state = {
  header: [],
  body: {},
  address: '',
  loading: false,
  detailTotalSize: null,
  paginationLoading: false,
  callText: []
}

const getters = {}

const mutations = {
  // 获取通话量详情头部
  [type.DETAILS_HEADER] (state, header) {
    state.header = header
  },
  // 获取通话量详情表格数据
  [type.DETAILS_BODY] (state, body) {
    state.body = body
  },
  // 获取通话录音地址
  [type.CALL_ADDRESS] (state, data) {
    state.address = data
  },
  // 表格加载状态
  [type.TABLE_LOADING_TYPE] (state, val) {
    state.loading = val
  },
  // 分页加载状态
  [type.PAGINATION_LOADING] (state, val) {
    state.paginationLoading = val
  },
  // 详情页总条数
  [type.DETAIL_TOTAL_SIZE] (state, size) {
    state.detailTotalSize = size
  },
  //获取录音转文字的内容
  [type.CALL_TEXT] (state, val) {
    state.callText = val
  }
}

const actions = {
  /**
   * @author 陈涛
   * @information 获取通话量详情头部数据
   * @date 2019-1-14
   * @param {*} commit 提交到mutation的数据
   */
  // 获取通话量详情头部
  getCallAmountInfoHeader ({ commit }) {
    request.getCallAmountInfoHeader().then(res => {
      commit(type.DETAILS_HEADER, res.data)
    })
  },
  /**
   * @author 陈涛
   * @information 获取通话量详情表格数据
   * @date 2019-1-14
   * @param {*} commit 提交到mutation的数据
   */
  // 获取通话量详情表格数据
  getCallAmountInfoBody ({ commit }, data) {
    // 表格加载状态
    commit(type.TABLE_LOADING_TYPE, true)
    request.getCallAmountInfoBody(data).then(res => {
      commit(type.DETAILS_BODY, res.data)
      // 表格加载状态
      commit(type.TABLE_LOADING_TYPE, false)
      // 分页加载状态
      commit(type.PAGINATION_LOADING, false)
    }).catch((error) => {
      commonUtils.setMessage('error', error.message)
      // 表格加载状态
      commit(type.TABLE_LOADING_TYPE, false)
      // 分页加载状态
      commit(type.PAGINATION_LOADING, false)
    })
  },
  /**
   * @author 陈涛
   * @information 获取通话录音地址
   * @date 2019-1-25
   * @param {*} commit 提交到mutation的数据
   */
  getCallRecording ({ dispatch,commit }, data) {
    return new Promise((resolve, reject) => {
      request.getCallRecording(data).then(res => {
        commit(type.CALL_ADDRESS, res.data)
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * @author 赵美阳
   * @information 获取通话录音转文字
   * @date 2019-4-10
   * @param {*} commit 提交到mutation的数据
   */
  getCallText({commit},data){
    request.getCallText(data).then(res=>{
      commit(type.CALL_TEXT,res.data.result.text)
    })
  },
  /**
   * @author 王磊
   * @information 分页
   * @date 2019-1-25
   * @param {*} commit 提交到mutation的数据
   */
  callAmountInfoPageModel ({ dispatch, commit }, model) {
    // 分页加载状态
    commit(type.PAGINATION_LOADING, true)
    dispatch('getCallAmountInfoBody', model)
  },
  /**
   * @author 王磊
   * @information 总条数
   * @date 2019-3-14
   * @param {*} commit 提交到mutation的数据
   */
  detailTotalSize ({ commit }, model) {
    request.detailTotalSize(model).then(res => {
      commit(type.DETAIL_TOTAL_SIZE, res.data.result)
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
